package appx.craft.droid.uc;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
import appx.craft.droid.R;
import appx.craft.droid.bean.RsvCellBean;
import appx.craft.droid.bean.RsvTableBean;
import appx.craft.droid.utils.UtilityHelper;

public class RsvpListAdapter extends BaseAdapter{

	private ArrayList<RsvTableBean> mTempArrayList;
	private LayoutInflater mLayoutInflater;
	private ITableCellListener mCellCallBack;
	private Context mContext;
	public RsvpListAdapter(Context vContext){
		mContext = vContext;
		mTempArrayList = new ArrayList<RsvTableBean>();
		mLayoutInflater = LayoutInflater.from(vContext);
		mCellCallBack = (ITableCellListener)vContext;
	}
	
	public void addTableItem(RsvTableBean v){
		if(mTempArrayList != null){
			mTempArrayList.add(v);
		}
	}
	
	public void addTableItems(ArrayList<RsvTableBean> vTempTableBean){
		
		if(!mTempArrayList.isEmpty()){
			mTempArrayList.clear();
		}
		
		for(RsvTableBean objTableBean : vTempTableBean){
			mTempArrayList.add(objTableBean);
		}
	}
	
	@Override
	public int getCount() {
		return mTempArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return mTempArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		final ContentHolder mContentHolder;
		
		RsvTableBean mTableBean = mTempArrayList.get(position);
		
		if(convertView == null){
			
			mContentHolder = new ContentHolder();
			
			convertView = mLayoutInflater.inflate(R.layout.row_rsv_table_cell,null);
			
			mContentHolder.mToggleOnoff = (ToggleButton)convertView.findViewById(R.id.toggle_rsv);
			
			mContentHolder.mTextView_Header = (TextView)convertView.findViewById(R.id.text_view_header_id);
			
			mContentHolder.mTextView_Header_Message = (TextView)convertView.findViewById(R.id.text_view_header_message);
			
			mContentHolder.mTextView_list_header_message = (TextView)convertView.findViewById(R.id.text_view_list_header_message);
			
			mContentHolder.mContainer_cells = (ViewGroup)convertView.findViewById(R.id.content_row_cells_id);
			
			convertView.setTag(mContentHolder);
			
		}else{
			
			mContentHolder = (ContentHolder)convertView.getTag();
		}
		
		if(mTableBean != null){
			
			if(mContentHolder != null){
				
				// cell header 
				
				mContentHolder.mTextView_Header.setText(mTableBean.getCellHeader());
				
				mContentHolder.mTextView_Header_Message.setText(mTableBean.getCellHeaderMessage());
				
				mContentHolder.mTextView_list_header_message.setText(mTableBean.getCelllistHeaderMessage());
				
				if(mTableBean.isToggleSwitch()){
					
					mContentHolder.mContainer_cells.setVisibility(View.VISIBLE);
					
				}else{
					
					mContentHolder.mContainer_cells.setVisibility(View.INVISIBLE);
				}
				
				mContentHolder.mToggleOnoff.setChecked(mTableBean.isToggleSwitch());
				
				mContentHolder.mToggleOnoff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				        if (isChecked) {
				            // The toggle is enabled
				        	mContentHolder.mTextView_list_header_message.setVisibility(View.VISIBLE);
				        	mContentHolder.mContainer_cells.setVisibility(View.VISIBLE);
				        } else {
				        	
				            // The toggle is disabled
				        	mContentHolder.mTextView_list_header_message.setVisibility(View.GONE);
				        	mContentHolder.mContainer_cells.setVisibility(View.GONE);
				        }
				    }
				});
				
				
				for (int i = 0; i < mTableBean.getmArrayList_cellBean().size(); i++) {
					
					final RsvCellBean mTempCellBean = mTableBean.getmArrayList_cellBean().get(i);
					
					if(i == 0){
						
						final View mTopCellView = mLayoutInflater.inflate(R.layout.row_rsv_cell_top,null);
						
						final ImageView image_view_item = (ImageView)mTopCellView.findViewById(R.id.image_view_item);
						image_view_item.setImageBitmap(UtilityHelper.getRoundedCornerBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ic_launcher), 15));
						
						final TextView mCellItemText = (TextView)mTopCellView.findViewById(R.id.text_view_cell_id);
						mCellItemText.setText(mTempCellBean.getRowText());
						
						
						final TextView mCellItemText_2 = (TextView)mTopCellView.findViewById(R.id.text_view_cell_id_2);
						mCellItemText_2.setText(mTempCellBean.getRowText1());
						
						final CheckBox  mItemChecked = (CheckBox)mTopCellView.findViewById(R.id.check_box_items);
						if(!mTempCellBean.isItemChecked)
						{
							mItemChecked.setVisibility(View.INVISIBLE);
							
						}else{
							mItemChecked.setVisibility(View.VISIBLE);
						}
							
						mTopCellView.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								
								if(mItemChecked.isChecked()){
									mItemChecked.setChecked(false);
									mItemChecked.setVisibility(View.INVISIBLE);
								}else{
									mItemChecked.setChecked(true);
									mItemChecked.setVisibility(View.VISIBLE);
								}
								mCellCallBack.onTableCellItemClick(Integer.parseInt(mTempCellBean.getRowID()),position);
							}
						});
						
						
						mContentHolder.mContainer_cells.addView(mTopCellView);
						
					}else if(i == (mTableBean.getmArrayList_cellBean().size()-1)){
						
						final View mBottomCellView = mLayoutInflater.inflate(R.layout.row_rsv_cell_bottom,null);
						
						final ImageView image_view_item = (ImageView)mBottomCellView.findViewById(R.id.image_view_item);
						image_view_item.setImageBitmap(UtilityHelper.getRoundedCornerBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ic_launcher), 15));
						
						final TextView mCellItemText = (TextView)mBottomCellView.findViewById(R.id.text_view_cell_id);
						mCellItemText.setText(mTempCellBean.getRowText());
						
						final TextView mCellItemText_2 = (TextView)mBottomCellView.findViewById(R.id.text_view_cell_id_2);
						mCellItemText_2.setText(mTempCellBean.getRowText1());
						
						final CheckBox  mItemChecked = (CheckBox)mBottomCellView.findViewById(R.id.check_box_items);
						if(!mTempCellBean.isItemChecked)
						{
							mItemChecked.setVisibility(View.INVISIBLE);
							
						}else{
							mItemChecked.setVisibility(View.VISIBLE);
						}
						
						mBottomCellView.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								if(mItemChecked.isChecked()){
									mItemChecked.setChecked(false);
									mItemChecked.setVisibility(View.INVISIBLE);
								}else{
									mItemChecked.setChecked(true);
									mItemChecked.setVisibility(View.VISIBLE);
								}
								mCellCallBack.onTableCellItemClick(Integer.parseInt(mTempCellBean.getRowID()),position);
							}
						});
						
						mContentHolder.mContainer_cells.addView(mBottomCellView);
						
					}else{
						
						final View mMiddleCellView = mLayoutInflater.inflate(R.layout.row_rsv_cell_middle,null);
						
						final ImageView image_view_item = (ImageView)mMiddleCellView.findViewById(R.id.image_view_item);
						image_view_item.setImageBitmap(UtilityHelper.getRoundedCornerBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ic_launcher), 15));
						
						final TextView mCellItemText = (TextView)mMiddleCellView.findViewById(R.id.text_view_cell_id);
						mCellItemText.setText(mTempCellBean.getRowText());
						
						final TextView mCellItemText_2 = (TextView)mMiddleCellView.findViewById(R.id.text_view_cell_id_2);
						mCellItemText_2.setText(mTempCellBean.getRowText1());
						
						final CheckBox  mItemChecked = (CheckBox)mMiddleCellView.findViewById(R.id.check_box_items);
						if(!mTempCellBean.isItemChecked)
						{
							mItemChecked.setVisibility(View.INVISIBLE);
							
						}else{
							mItemChecked.setVisibility(View.VISIBLE);
						}
						
						mMiddleCellView.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								
								if(mItemChecked.isChecked()){
									mItemChecked.setChecked(false);
									mItemChecked.setVisibility(View.INVISIBLE);
								}else{
									mItemChecked.setChecked(true);
									mItemChecked.setVisibility(View.VISIBLE);
								}
								mCellCallBack.onTableCellItemClick(Integer.parseInt(mTempCellBean.getRowID()),position);
							}
						});
						
						mContentHolder.mContainer_cells.addView(mMiddleCellView);
					}
				}
			}
		}
		return convertView;
	}
	
	class ContentHolder{
		
		
		ToggleButton mToggleOnoff;
		TextView mTextView_Header_Message;
		TextView mTextView_list_header_message;
		TextView mTextView_Header;
		ViewGroup mContainer_cells;
	}
	
}
